<?php

// +----------------------------------------------------------------------
// | 瞎猜订阅功能 2017-10-9
// +----------------------------------------------------------------------

class UserSubscribeRedisService extends BaseRedisService {

    var $user_subscribe_db; //set有序数据=user_id  用户关注的瞎猜ID
    var $user_subscribe_by_db; // set有序数据=user_id  关注用户的会员ID
//    var $user_hash_db; //所有会员数据 user_id hash数据 存储在线数据
    //var $user_db; //:user_id  hash数据，会员数据
    private $id;

    /**
      +----------------------------------------------------------
     * 架构函数
      +----------------------------------------------------------
     * @access public
      +----------------------------------------------------------
     */
    public function __construct($userID) {

        parent::__construct();
        $this->user_subscribe_db = $this->prefix . 'xiacai_subscribe:';
        $this->user_subscribe_by_db = $this->prefix . 'xiacai_subscribe_by:';
        $this->id = $userID;
    }

    /**
     * 订阅瞎猜
     * @param type $xiacai_id
     */
    public function subscribe($xiacai_id = 0) {
        $this->redis->sAdd($this->user_subscribe_db . $this->id, $xiacai_id); // xiacai_subscribe:100512 => 2, 5 表示 100512 订阅了 2, 5
        $this->redis->sAdd($this->user_subscribe_by_db . $xiacai_id, $this->id); // xiacai_subscribe_by:2 =>  表示 2 被 100512 订阅了
        if ($xiacai_id) {
            $this->redis->hIncrBy($this->xiacai_db . $xiacai_id, 'subscribe_count', 1);
        }
    }

    /**
     * 取消订阅瞎猜
     * @param type $xiacai_id
     */
    public function unsubscribe($xiacai_id = 0) {
        if ($this->is_subscribe($xiacai_id)) {
            $this->redis->srem($this->user_subscribe_db . $this->id, $xiacai_id);
            $this->redis->srem($this->user_subscribe_by_db . $xiacai_id, $this->id);

            if ($xiacai_id) {
                //$this->incry($this->xiacai_db.$room_id, 'fans_count', -1);
                $this->redis->hIncrBy($this->xiacai_db . $xiacai_id, 'subscribe_count', -1);
            }
        }
    }

    /**
     * 获取已订阅的瞎猜列表
     * @return type
     */
    public function get_subscribe_list() {
        return $this->redis->sMembers($this->user_subscribe_db . $this->id);
    }

    /**
     * 获取未订阅的瞎猜列表
     */
    public function get_unsubscribe_list() {
        $this->redis->sMembers($this->xiacai_db);
    }

    /*
     * Returns an array of user ID's that this user is subscribed by.
     */

    public function subscribe_by($user_id = 0) {
        if ($user_id == 0)
            $user_id = $this->id;
        return $this->redis->sMembers($this->user_subscribe_by_db . $user_id);
    }

    /**
     * 返回用户是否有订阅指定的瞎猜
     * @param type $xiacai_id
     * @return type
     */
    public function is_subscribe($xiacai_id) {
        return $this->redis->sismember($this->user_subscribe_db . $this->id, $xiacai_id);
    }

    /**
     * 返回指定的瞎猜是否被当前用户订阅
     * @param type $xiacai_id
     * @return type
     */
    public function is_subscribe_by($xiacai_id) {
        return $this->redis->sismember($this->user_subscribe_by_db . $xiacai_id, $this->id);
    }

    /**
     * 返回用户与瞎猜是否有订阅关系
     * @param type $xiacai_id
     * @return type
     */
    public function is_mutual($xiacai_id) {
        return ($this->is_subscribe($xiacai_id) && $this->is_subscribe_by($xiacai_id));
    }

    /**
     * 获取个人订阅总数
     * @return type
     */
    public function subscribe_count() {
        return intval($this->redis->sCard($this->user_subscribe_db . $this->id));
    }

    /**
     * 获取某个瞎猜被订阅的总数
     * @param type $xiacai_id
     * @return type
     */
    public function subscribeer_count($xiacai_id) {
        return intval($this->redis->sCard($this->user_subscribe_by_db . $xiacai_id));
    }

    /*
     * Finds all users that the given users subscribe in common.
     * Returns an array of user IDs
     */

    public function common_subscribe($users) {
        $redis = $this->redis;
        $users[] = $this->id;

        $keys = array();
        foreach ($users as $user) {
            $keys[] = $this->user_subscribe_db . $user;
        }

        return $redis->sinter($keys);
    }

    /*
     * Finds all users that all of the given users are subscribed by in common.
     * Returns an array of user IDs
     */

    public function common_subscribe_by($users) {
        $redis = $this->redis;


        $keys = array();
        foreach ($users as $user) {
            $keys[] = $this->user_subscribe_by_db . $user;
        }
        $keys[] = $this->user_subscribe_db . $this->id;

        return $redis->sinter($keys);
    }

    //建立set数组
    public function test_new() {
        $key = $this->user_subscribe_db . '77';
        $this->redis->zAdd($key, 22, 2);
        $this->redis->zAdd($key, 33, 3);
    }

    public function zsubscribe() {
        return $this->redis->zRange($this->user_subscribe_db . $this->id, 0, -1);
    }

}

//类定义结束
?>